home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 676 / fbm / fbm10ami.zoo / rs2fbm.c < prev    next >
C/C++ Source or Header  |  1992-05-07  |  5KB  |  204 lines

  1. /* rs2fbm.c */
  2. /* Convert rayshade (3.0 or 4.0) output into fbm format */
  3. /* use -3 option for rayshade 3.0 format */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define ILL_PAR 102
  8. #define NO_MEM 100
  9. #define NOT_INP 104
  10. #define NOT_OUT 105
  11.  
  12. typedef unsigned char UBYTE;
  13. typedef unsigned long ULONG;
  14. typedef unsigned short UWORD;
  15.  
  16. #ifdef LATTICE_50
  17. #define ANSI_C
  18. #endif
  19.  
  20. #ifdef ANSI_C
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. void WriteRGBplanes(FILE *, UWORD, UWORD);
  25. #else
  26. extern void *malloc();
  27. void WriteRGBplanes();
  28. #endif
  29.  
  30. UBYTE *BitMap[1024];
  31.  
  32. # define FBM_MAX_TITLE        80        /* For title and credits */
  33.  
  34. # define BLACK            0        /* For 8bit files */
  35. # define WHITE            255        /* For 8bit files */
  36. # define BYTE            256        /* For 8bit files */
  37.  
  38. # define BLANKS        "                                             "
  39.  
  40. # define FBM_MAGIC    "%bitmap"
  41.  
  42. /* FBM bitmap headers in files (null terminated 12 character ascii strings) */
  43. typedef struct fbm_filehdr_struct {
  44.     char    magic[8];        /* 8 bytes FBM_MAGIC number */
  45.     char    cols[8];        /* Width in pixels */
  46.     char    rows[8];        /* Height in pixels */
  47.     char    planes[8];        /* Depth (1 for B+W, 3 for RGB) */
  48.     char    bits[8];        /* Bits per pixel */
  49.     char    physbits[8];        /* Bits to store each pixel */
  50.     char    rowlen[12];        /* Length of a row in bytes */
  51.     char    plnlen[12];        /* Length of a plane in bytes */
  52.     char    clrlen[12];        /* Length of colormap in bytes */
  53.     char    aspect[12];        /* ratio of Y to X of one pixel */
  54.     char    title[FBM_MAX_TITLE];    /* Null terminated title */
  55.     char    credits[FBM_MAX_TITLE];    /* Null terminated credits */
  56. } FBMFILEHDR;
  57.  
  58. void WriteRGBplanes(fpo, yres, rowlen)
  59. UWORD yres;
  60. UWORD rowlen;
  61. FILE *fpo;
  62. {
  63.   register UWORD i,k;
  64.  
  65.   /* Write RED plane */
  66.   for (i=0; i<yres; i++) {
  67.     for (k=0; k<rowlen*3; k += 3) {
  68.       putc(*(BitMap[i] +(ULONG)(k)),fpo);
  69.     };
  70.   };
  71.   /* Write GREEN plane */
  72.   for (i=0; i<yres; i++) {
  73.     for (k=0; k<rowlen*3; k += 3) {
  74.       putc(*(BitMap[i] +(ULONG)(k+1)),fpo);
  75.     };
  76.   };
  77.   /* Write BLUE plane */
  78.   for (i=0; i<yres; i++) {
  79.     for (k=0; k<rowlen*3; k += 3) {
  80.       putc(*(BitMap[i] +(ULONG)(k+2)),fpo);
  81.     };
  82.   };
  83. }
  84.  
  85. void Usage(CmdName)
  86. char *CmdName;
  87. {
  88.   fprintf(stderr,"Usage: %s [-3] inputfile [outputfile]\n",CmdName);
  89.   fprintf(stderr,"converts rayshade or mtv input to FBM format\n");
  90. }
  91.  
  92. main(argc,argv)
  93. int argc;
  94. char *argv[];
  95. {
  96.   register short i,k;
  97.   char *argstr;
  98.   FILE *fpi, *fpo;
  99.   static char InName[128], OutName[128];
  100.   FBMFILEHDR *fbm_hdr;
  101.   UWORD xres, yres;
  102.   UWORD rowlen;
  103.   char rs3fmt = 0;
  104.  
  105.   k=0;
  106.   if (argc<2) {
  107.     Usage(argv[0]);
  108.     exit(2);
  109.   };
  110.   for (i=1; i<argc; i++) {
  111.     argstr = argv[i];
  112.     if (*argstr == '-') {
  113.       argstr++;
  114.       if (*argstr == 'h') { /* help wanted */
  115.         Usage(argv[0]);
  116.         exit(0);
  117.       }
  118.       if (*argstr == '3') { /* help wanted */
  119.         rs3fmt = !0;
  120.       }
  121.       else fprintf(stderr,"Unknown option %s ignored\n",argv[i]);
  122.     }
  123.     else {
  124.       k++;
  125.       if (k==1) strcpy(InName,argstr);
  126.       else if (k==2) strcpy(OutName,argstr);
  127.       else fprintf(stderr,"Extraneous argument %s ignored\n",argstr);
  128.     }
  129.   };
  130.   /* Args are parsed, let's do our job */
  131.   if ((fpi=fopen(InName,"r")) == NULL) {
  132.     fprintf(stderr,"Cannot open file %s for input\n",InName);
  133.     exit(NOT_INP);
  134.   };
  135.  
  136.   fscanf(fpi,"%hd %hd",&xres,&yres);
  137.   if (yres > 1024) {
  138.     fclose(fpi);
  139.     fprintf(stderr,"Cannot handle images with more than 1024 rows\n");
  140.     exit(20);
  141.   };
  142.   if (getc(fpi) != '\n') {
  143.     fclose(fpi);
  144.     fprintf(stderr,"A newline char should have been found after the header\n");
  145.     fprintf(stderr,"Image is probably not in RS - format\n");
  146.     exit(20);
  147.   };
  148.  
  149.   rowlen = 2 * ((xres * 8 + 15) / 16);
  150.   
  151.   for (i=0; i<yres; i++) {
  152.     if ((BitMap[i] = malloc((unsigned)rowlen*3)) == NULL) {
  153.       fprintf(stderr,"not enough memory for malloc()\n");
  154.       exit(101);
  155.     };
  156.     setmem(BitMap[i],(unsigned)rowlen*3,0);
  157.   };
  158.  
  159.   if (rs3fmt) {
  160.     for (i=0; i<yres; i++) {
  161.       fread(BitMap[i],1,(unsigned)xres*3,fpi);
  162.     };
  163.   }
  164.   else {
  165.     for (i=yres-1; i>=0; i--) {
  166.       fread(BitMap[i],1,(unsigned)xres*3,fpi);
  167.     };
  168.   };
  169.  
  170.   fclose(fpi);
  171.  
  172.   if (k==1) fpo = stdout;
  173.   else if ((fpo=fopen(OutName,"w")) == NULL) {
  174.     fprintf(stderr,"Cannot open file %s for output\n",OutName);
  175.     exit(NOT_OUT);
  176.   };
  177.  
  178.   if ((fbm_hdr=malloc(sizeof(FBMFILEHDR))) == NULL) {
  179.     fprintf(stderr,"Not enough memory for malloc()\n");
  180.     exit(NO_MEM);
  181.   };
  182.  
  183.   strncpy(fbm_hdr->magic,FBM_MAGIC,8);
  184.   sprintf(fbm_hdr->cols,"%7u",(unsigned)xres);
  185.   sprintf(fbm_hdr->rows,"%7u",(unsigned)yres);
  186.   sprintf(fbm_hdr->planes,"%7u",3);
  187.   sprintf(fbm_hdr->bits,"%7u",8);
  188.   sprintf(fbm_hdr->physbits,"%7u",8);
  189.  
  190.   sprintf(fbm_hdr->rowlen,"%11u",(unsigned)rowlen);
  191.   sprintf(fbm_hdr->plnlen,"%11lu",(ULONG)rowlen*(ULONG)yres);
  192.   sprintf(fbm_hdr->clrlen,"%11u",0);
  193.   sprintf(fbm_hdr->aspect,"%11.6f",1.0);
  194.   strncpy(fbm_hdr->title,"",80);
  195.   strncpy(fbm_hdr->credits,"",80);
  196.   fwrite(fbm_hdr,1,sizeof(FBMFILEHDR),fpo);
  197.  
  198.   fprintf(stderr,"Writing  %u * %u * 3  FBM-Bitmap\n",(unsigned)rowlen,(unsigned)yres);
  199.   WriteRGBplanes(fpo,yres,rowlen);
  200.   fclose(fpo);
  201.   free(fbm_hdr);
  202.   for (i=0; i<yres; i++) free(BitMap[i]);
  203. }
  204.